//js基本语法
document.write("用js输入html页面!")
alert("这是个弹窗（外联脚本）");
console.log("此段是用js写入浏览器控制台的")
var test = 20;
test = "汉字";//var定义变量，可以转换
var test = 2.5;//可以重复定义
if(1)
{
    var a1 = "123";
    let a2 = 10;//let定义变量，仅在代码块中有效
}
document.write(a1, "我是代码块中用var定义的变量!");
const a3 = 20;  //重新赋值会报错
document.write(a3, "我是const定义的常量a3!")
alert(typeof a1);
var a4 = 10;
var a5 = "10";

document.write("  a4 = 10; a5 = \"10\"; a4 == a5结果是", a4 == a5);
document.write("  a4 === a5结果是", a4 === a5);

//类型转化
var trans = +"20";
document.write("  ", trans, typeof trans);//方法1：使用正号运算符
trans = "ABC";
document.write(" ", parseInt(trans), typeof trans);

//函数
function add(q1, q2)
{
    return q1 + q2;
}

var q1 = 5, q2 = 6;

document.write("  ",q1, "+", q2, "=", add(q1,q2));

//数组
var arr = [0, 1, 2, 3, 4, 5]//用[]定义
var i = 0;
while(i < 6)
{
    document.write(" ", arr[i]);
    i++;
}
arr[10] = "im a string ";//数组可变
document.write(" arr[9]=", arr[9], " arr[10]=", arr[10]);

document.write(" arr元素数目为:", arr.length);

//字符串
var str = "i am a string for example";
var character = str.charAt(7);
document.write(str, "  charAt(7) = ", character);

//自定义对象
var person = {
    Name : "Mike",
    age : 23,
    eat : function()
    {
        document.write(person.Name, "在吃饭!");
    }
};

person.eat();
